package com.aijk.web.admin.controller.openapi;

import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.ehm.api.*;
import com.aijk.ehm.model.*;
import com.aijk.ehm.model.qo.EhmClassifySubscribeQo;
import com.aijk.ehm.model.qo.EhmInfoClassifyQo;
import com.aijk.ehm.model.qo.EhmInfoQo;
import com.aijk.web.admin.controller.openapi.entity.ApiResponse;
import com.aijk.web.admin.entity.EvaAddDto;
import com.aijk.web.admin.model.ClassifySubscribeDto;
import com.aijk.web.admin.model.InfoClassifyGetVo;
import com.aijk.web.admin.model.MhInfoExt;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.*;

@Component
@Controller
public class InformationController extends ApiBaseController {

    private static final Logger logger = LoggerFactory.getLogger(InformationController.class);

    @Autowired
    private EhmInfoClassifyApi ehmInfoClassifyApi;

    @Autowired
    private EhmInfoApi ehmInfoApi;

    @Autowired
    private EhmClassifySubscribeApi ehmClassifySubscribeApi;

    @Autowired
    private EhmInfoSrcApi ehmInfoSrcApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    @Autowired
    private EhmInfoEvaluateApi ehmInfoEvaluateApi;




    /**
     * 资讯类型获取
     * @param resident
     * @return
     */
    @RequestMapping(path = "app/info/classify/get", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object companyList(@Valid EhmResident resident) {
        Map<Long,EhmClassifySubscribe> ehmClassifySubscribeMap = new HashMap<>();

        EhmInfoClassifyQo ehmInfoClassifyQo = new EhmInfoClassifyQo();
        ehmInfoClassifyQo.setStatus((short) 1);
        ehmInfoClassifyQo.setSortway("sort_number asc");
        List<EhmInfoClassify> ehmInfoClassifies = ehmInfoClassifyApi.list(ehmInfoClassifyQo);
        Long type =0l;

        if(resident.getId()>0){
            EhmClassifySubscribeQo ehmClassifySubscribeQo = new EhmClassifySubscribeQo();
            ehmClassifySubscribeQo.setCreater(resident.getId());
            List<EhmClassifySubscribe> classifySubscribes = ehmClassifySubscribeApi.list(ehmClassifySubscribeQo);
            if (classifySubscribes != null && classifySubscribes.size()>0){
                for(EhmClassifySubscribe  tagsSubscribe: classifySubscribes){
                    ehmClassifySubscribeMap.put(tagsSubscribe.getCid(),tagsSubscribe);
                }
                if (classifySubscribes.get(0).getType()==1){
                    type =1l;
                }
            }
        }

        List<InfoClassifyGetVo> infoClassifyGetVos = new ArrayList<>();

        if (type ==1l){
            for(EhmInfoClassify tags : ehmInfoClassifies){
                InfoClassifyGetVo infoClassifyGetVo = new InfoClassifyGetVo();
                infoClassifyGetVo.setId(tags.getId());
                infoClassifyGetVo.setTitle(tags.getTitle());
                infoClassifyGetVo.setSort(tags.getSortNumber());
                infoClassifyGetVo.setSubscribe(0);
                infoClassifyGetVos.add(infoClassifyGetVo);
            }
        }else {
            for(EhmInfoClassify tags : ehmInfoClassifies){
                InfoClassifyGetVo infoClassifyGetVo = new InfoClassifyGetVo();
                infoClassifyGetVo.setId(tags.getId());
                infoClassifyGetVo.setTitle(tags.getTitle());
                infoClassifyGetVo.setSort(tags.getSortNumber());
                infoClassifyGetVo.setSubscribe( (ehmClassifySubscribeMap.size() ==0 || ehmClassifySubscribeMap.containsKey(tags.getId())) ? 1 : 0);
                infoClassifyGetVos.add(infoClassifyGetVo);
            }
        }

        ApiResponse response = new ApiResponse();
        response.setData(infoClassifyGetVos);
        return response;
    }



    /**
     * 订阅类型
     * @param
     * @return
     */
    @RequestMapping(path = "app/info/classify/subscribe", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object subscribe(@RequestParam String data ,@RequestParam Long residentId) {
        List<Long> tids = new ArrayList<>();

        if (!data.contains("id")){
            EhmClassifySubscribeQo ehmClassifySubscribeQo = new EhmClassifySubscribeQo();
            ehmClassifySubscribeQo.setCreater(residentId);
            ehmClassifySubscribeApi.delete(ehmClassifySubscribeQo);
            EhmClassifySubscribe subscribe = new EhmClassifySubscribe();
            EhmResident ehmResident = ehmResidentApi.findOne(residentId);
            subscribe.setType(1);
            subscribe.setCreater(residentId);
            subscribe.setCreated(new Date());
            subscribe.setCreaterUser(ehmResident.getTitle());
            ehmClassifySubscribeApi.save(subscribe);
            ApiResponse response = new ApiResponse();
            return response;
        }

        List<ClassifySubscribeDto> tagsSubscribeDtoList = JSONObject.parseArray(data, ClassifySubscribeDto.class);
        for(ClassifySubscribeDto dto: tagsSubscribeDtoList){
            if(dto.getId() != null && dto.getId() > 0){
                tids.add(dto.getId());
            }
        }

        ehmClassifySubscribeApi.saves(tids, residentId);

        ApiResponse response = new ApiResponse();
        return response;
    }



    /**
     * 获取资讯列表
     * @param ehmInfoQo
     * @return
     */
    @RequestMapping(path = "app/info/list/get", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object infolist(@Valid EhmInfoQo ehmInfoQo) {
        ehmInfoQo.setStatus((short) 0);
        Long page_no = ehmInfoQo.getPageNo() == null ? 1:ehmInfoQo.getPageNo();
        Long page_size = ehmInfoQo.getPageSize() == null ? 20 : ehmInfoQo.getPageSize();
        ehmInfoQo.setPageSize(page_size);
        ehmInfoQo.setPageNo(page_no);
        List<EhmInfo> ehmInfos = ehmInfoApi.list(ehmInfoQo);
        String url = "http://jbzgjk.aijk.com/h5/news/browse/";

        List<MhInfoExt> list = new ArrayList<>();

        if (ehmInfos != null && ehmInfos.size() > 0) {
            list = PojoUtil.convert(ehmInfos, MhInfoExt.class);
            for (MhInfoExt mhMemberExt : list) {
                if (mhMemberExt.getClassifyId() != null){
                    EhmInfoClassify mhInfoClassify = ehmInfoClassifyApi.findOne(mhMemberExt.getClassifyId());
                    if (mhInfoClassify != null){
                        mhMemberExt.setClassify(mhInfoClassify.getTitle());
                    }
                }
                if (mhMemberExt.getSrcId() != null){
                    EhmInfoSrc mhInfoSrc = ehmInfoSrcApi.findOne(mhMemberExt.getSrcId());
                    if (mhInfoSrc != null){
                        mhMemberExt.setSrc(mhInfoSrc.getTitle());
                    }
                }
                if (mhMemberExt.getHrefType() == 1){
                    String h5Url= url + mhMemberExt.getId();
                    mhMemberExt.setHrefUrl(h5Url);
                }
            }
        }
        long total_results = new PageInfo(ehmInfos).getTotal();
        Boolean hasnext = page_no * page_size < total_results ? true : false;
        ApiResponse response = new ApiResponse();
        HashMap map = new HashMap();
        map.put("hasnext", hasnext);
        map.put("total_results", total_results);
        map.put("news",list);
        response.setData(map);
        return response;
    }



    /**
     * 资讯获取
     * @param ehmInfoQo
     * @return
     */
    @RequestMapping(path = "app/info/get", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object infoget(@Valid EhmInfoQo ehmInfoQo) {
        EhmInfo ehmInfo = ehmInfoApi.findOne(ehmInfoQo.getId());
        ehmInfo.setScanNum(ehmInfo.getScanNum() + 1);
        ehmInfoApi.update(ehmInfo);

        ApiResponse response = new ApiResponse();
        response.setData(ehmInfo);
        return response;
    }

    /**
     * 资讯评论新增
     * @param
     * @return
     */
    @RequestMapping(path = "app/info/eva/save", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object eva(@Valid EvaAddDto evaAddDto) {
        if(StringUtils.isEmpty(evaAddDto.getNid()) || StringUtils.isEmpty(evaAddDto.getRid()))
            return  new ApiResponse("-1","参数异常");

        EhmResident ehmResident = ehmResidentApi.findOne(evaAddDto.getRid());
        EhmInfoEvaluate ehmInfoEvaluate = new EhmInfoEvaluate();
        ehmInfoEvaluate.setNewsid(evaAddDto.getNid());
        ehmInfoEvaluate.setContent(evaAddDto.getContent());
        ehmInfoEvaluate.setEvaluateTime(new Date());
        ehmInfoEvaluate.setUserId(ehmResident.getId());
        ehmInfoEvaluate.setAvatar(ehmResident.getImage());
        ehmInfoEvaluate.setCellphone(ehmResident.getCellphone());
        ehmInfoEvaluate.setName(ehmResident.getTitle());
        ehmInfoEvaluate.setSex(ehmResident.getSex());
        ehmInfoEvaluate.setAge(ehmResident.getAge());
        ehmInfoEvaluateApi.save(ehmInfoEvaluate);


        ApiResponse response = new ApiResponse();
        return response;
    }

}
